package com.lujianfei.powersearch.ui.report.viewmodel

import android.util.Log
import androidx.databinding.ObservableArrayList
import androidx.lifecycle.ViewModel
import com.lujianfei.powersearch.database.SearchItemModel
import com.lujianfei.powersearch.ui.report.model.ReportItemModel
import com.lujianfei.powersearch.ui.report.model.ReportItemType
import com.orm.SugarRecord

class TotalConsumeReportViewModel : ViewModel() {

    var mSearchItemModel: SearchItemModel? = null

    val data = ObservableArrayList<ReportItemModel>()

    val tempList = arrayListOf<SearchItemModel>()

    val ids = HashSet<Long>()

    fun generateReport() {
        traversal(root = mSearchItemModel, level = 0)

        ids.clear()
        // 添加标题
        data.add(ReportItemModel(content = mSearchItemModel?.name?:"", type = ReportItemType.NameTitle, cost = ""))
        tempList.sortByDescending { it.sort }
        tempList.filter { it.level == 0 && !it.cost.isNullOrBlank() }.let { level0s->
            level0s.forEach {level0->
                data.add(ReportItemModel(content = level0.name?:"", type = ReportItemType.MainTitle, cost = level0.cost?:""))
                processLevel0(level0)
            }
        }
        val totalCost = data.filter { it.type == ReportItemType.MainTitle }.sumOf { it.cost.toDouble() }
        data.add(
            ReportItemModel(
                content = "总费用",
                cost = costDisplay(totalCost),
                type = ReportItemType.MainTitle
            )
        )
    }

    private fun processLevel0(level0: SearchItemModel) {
        tempList.filter { it.level == 1 && it.parentId == level0.id && !it.cost.isNullOrBlank() }
            .let { level1s ->
                level1s.forEach { level1 ->
                    data.add(
                        ReportItemModel(
                            content = level1.name ?: "",
                            type = ReportItemType.SecondTitle,
                            cost = level1.cost ?: ""
                        )
                    )
                    processLevel1(level1)
                }
            }
    }

    private fun processLevel1(level1: SearchItemModel) {
        tempList.filter { it.level == 2 && it.parentId == level1.id && !it.cost.isNullOrBlank() }
            .let { level2s ->
                level2s.forEach { level2 ->
                    data.add(
                        ReportItemModel(
                            content = level2.name ?: "",
                            type = ReportItemType.ThirdTitle,
                            cost = level2.cost ?: ""
                        )
                    )
                    processLevel2(level2)
                }
            }
    }

    private fun processLevel2(level2: SearchItemModel) {
        tempList.filter { it.level == 3 && it.parentId == level2.id && !it.cost.isNullOrBlank() }
            .let { level3s ->
                level3s.forEach { level3 ->
                    data.add(
                        ReportItemModel(
                            content = level3.name ?: "",
                            type = ReportItemType.ThirdTitle,
                            cost = level3.cost ?: ""
                        )
                    )
                }
            }
    }

    private fun costDisplay(cost:Double):String {
        kotlin.runCatching {
            return String.format("%.2f", cost)
        }
        return ""
    }

    private fun traversal(root: SearchItemModel?, level: Int) {
        if (root == null) return
        val models = SugarRecord.find(SearchItemModel::class.java, "parentId=?", "" + root.id)
        if (null != models && models.isNotEmpty()) {
            for (d in models) {
                d.level = level
                traversal(d, level + 1)
            }
            tempList.addAll(models)
        }
    }
}